﻿#pragma once
#include "yzrutil.h"
#include "DSPGroup.h"
namespace yzrilyzr_dsp{
	ECLASS(DSPParallel, public DSPGroup){
	public:
	u_sample procDsp(u_sample v) override{
		if(dsps.empty())return v;
		u_sample sum=0;
		for(auto & d : dsps){
			if(d == nullptr) continue;
			sum+=d->procDsp(v);
		}
		return sum;
	}
	void procBlock(u_sample * input, u_sample * output, u_index length)override{
		if(dsps.empty()){
			if(input == output)return;
			memcpy(output, input, sizeof(u_sample) * length);
			return;
		}
		u_sample * sum=new u_sample[length]{0};
		std::fill(output, output + length, 0);
		for(auto & d : dsps){
			if(d == nullptr) continue;
			d->procBlock(input, sum, length);
			for(u_index i=0;i < length;i++){
				output[i]+=sum[i];
			}
		}
	}
	DSPParallel():DSPGroup(){}
	DSPParallel(std::vector<std::shared_ptr<DSP>> &dsps1):DSPGroup(dsps1){	}
	
	std::shared_ptr<DSP> cloneDSP()override{
		std::vector<std::shared_ptr<DSP>> dsps1;
		for(auto & d : dsps){
			dsps1.push_back(std::shared_ptr<DSP>(d->cloneDSP()));
		}
		return std::make_shared <DSPParallel>(dsps1);
	}
	yzrilyzr_lang::String toString()const override;
	};
}